package com.example.myapplication.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.PointF;
import android.os.CountDownTimer;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.OkHttpClient;

public class Point{
    private MagneticUtil magnetic;         //磁场
    private WifiUtils wifi;                //wifi
    private float[] magneticValue = new float[3];
    private String wifiValue = "";
    private PointF result;

    public Thread thread;
    private static List<float[]> magneticList = new ArrayList<>();
    private  Context context;

    public Point(Context context, Activity activity){
        wifi = new WifiUtils(context,activity);
        magnetic = new MagneticUtil(context);
        magnetic.start();
        this.context = context;
    }

    public void getLocation(LocationCallback callback) {
        CountDownTimer timer = new CountDownTimer(2000,200) {
            @Override
            public void onTick(long l) {
                if(magneticList.size()<=10)
                {
                    magneticValue = magnetic.globalValues();
                    float[] ans = magneticValue.clone();
                    if(magneticList.size()!=0&&!isEqualArray(magneticList.get(magneticList.size() - 1), ans))
                        magneticList.add(ans);
                    else if(magneticList.size() == 0)
                        magneticList.add(ans);
                }
            }

            @Override
            public void onFinish() {

            }
        }.start();
        String p1 = null;
        for(float[] values : magneticList){
            if(p1 == null)
                p1 = String.valueOf(values[0]) + ":" + String.valueOf(values[1]) + ":" + String.valueOf(values[2]) + ";";
            else
                p1 += String.valueOf(values[0]) + ":" + String.valueOf(values[1]) + ":" + String.valueOf(values[2]) + ";";
        }
        magneticList.clear();
        wifiValue = wifi.getWifi().toString();

        if (!wifiValue.isEmpty() && p1 != null) {
            OkHttpUtils.get()
                    .url("http://121.36.92.128:8086/coordinate")
                    .addParams("wifi", wifiValue)
                    .addParams("load", p1)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            System.out.println(e.toString());
                            callback.onLocationError(e);
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            float[] location = new float[3];
                            if (!response.isEmpty()) {
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    float x = Float.parseFloat(jsonObject.optString("x"));
                                    float y = Float.parseFloat(jsonObject.optString("y"));
                                    float temp = Float.parseFloat(jsonObject.optString("zl"));
                                    int zl = (int) temp;
                                    location[0] = x;
                                    location[1] = y;
                                    location[2] = zl;
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                            callback.onLocationResult(location);
                        }
                    });
        } else {
            callback.onLocationResult(null);
        }
    }
    // 回调接口
    public interface LocationCallback {
        void onLocationResult(float[] location);
        void onLocationError(Exception e);
    }

    // 添加一个方法来比较两个数组是否相等
    private boolean isEqualArray(float[] array1, float[] array2)
    {
        if (array1.length != array2.length) {
            return false;
        }
        for (int i = 0; i < array1.length; i++)
        {
            if (array1[i] != array2[i]) {
                return false;
            }
        }
        return true;
    }

}